package com.luoxiang.lib_common.utils;

import android.os.Handler;
import android.os.Message;

import androidx.annotation.NonNull;

/**
 * @Description TODO
 * @systemUser Administrator
 * @Author LiangXin
 * @Date 08-23-2022 周二 10:08
 */
public class ThendTimeUtil {
    /**
     *
     * @param time 倒计时秒数
     * @param timeBack 回调接口
     */
    public static void Therd(@NonNull int time, @NonNull timeBack timeBack){
        Handler handler = new Handler(){
            @Override
            public void handleMessage(Message msg) {//为避免在回调接口直接刷新UI报错，所以使用了handler
                super.handleMessage(msg);
                timeBack.back(msg.what,msg.what==0?true:false);//通知页面
            }
        };
        new Thread(new Runnable() {
            @Override
            public void run() {
                int a = time;
                while (a!=-1){
                    try {
                        Message message = new Message();
                        message.what = a;
                        handler.sendMessage(message);
                        Thread.sleep(1000);
                        a--;
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }
    public interface timeBack{
        /**
         *
         * @param time 当前秒数
         * @param n 倒计时是否结束
         */
        void back(int time,boolean n);
    }


}
